#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool backspaceCompare(string s, string t) {
        stack<char> st1, st2;

        for (auto e : s) {
            if (e == '#' && !st1.empty()) {
                st1.pop();
            }
            else if (e == '#' && st1.empty())
                ;
            else {
                st1.emplace(e);
            }
        }

        for (auto e : t) {
            if (e == '#' && !st2.empty())
                st2.pop();
            else if (e == '#' && st2.empty())
                ;
            else
                st2.emplace(e);
        }

        if (st1 == st2) return true;
        return false;
    }
};